package  
{
	import de.popforge.audio.AudioEngine;
	import de.popforge.audio.Freeverb;
	import de.popforge.audio.LowPassLFO;
	import de.popforge.audio.MP3Extractor;
	import de.popforge.audio.PlayString;
	import de.popforge.audio.Plaything;
	import de.popforge.audio.SimpleDelay;
	import de.popforge.audio.SimpleFlanger;
	import de.popforge.audio.SynthSample;
	import de.popforge.audio.driver.AudioDriver;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;	

	/**
	 * A collection of AudioSandboxes to start with dealing Flash Audio DSP.
	 * 
	 * I tried it to make it as easy and readable as I can.
	 * Therefore the audio flow is only straight forward (linear).
	 * 
	 * Note:
	 * None of the source code is optimized in any way.
	 * No GUI is provided to avoid tons of nasty code.
	 * 
	 * http://session.andre-michelle.com/2009/fitc.amsterdam/AudioSandbox.zip
	 * 
	 * @author Andre Michelle (am@andre-michelle.com)
	 */
	public class Application extends Sprite 
	{
		private var _engine: AudioEngine;
		
		private var _peak: Shape;

		public function Application()
		{
			_engine = new AudioEngine( new MP3Extractor( '../mp3/example.mp3' ) );
//			_engine = new AudioEngine( new SynthSample() );
//			_engine = new AudioEngine( new Plaything() );
//			_engine = new AudioEngine( new PlayString() );

//			_engine.addEffect( new SimpleDelay( 320 * 44.1, .5, .6 ) ); // SimpleDelay (delayOffset, wetLevel, feedback)
//			_engine.addEffect( new SimpleFlanger( 4.0, 7200, .5, .6, .6, .8 ) ); // SimpleFlanger (delayMs, lfoSpeedMs, wetLevel ,dryLevel, feedback)
//			_engine.addEffect( new Freeverb( .8, .5 ) ); // Freeverb (feedback, damp)
//			_engine.addEffect( new LowPassLFO( 7000, .8, 50, 7000 ) ); // LowPassLFO (lfoSpeedMs, resonance, minFreqHz, maxFreqHz)

			_peak = new Shape();
			 addChild( _peak );

			stage.addEventListener( Event.ENTER_FRAME, enterFrame );
			stage.addEventListener( Event.RESIZE, resize );
			stage.frameRate = 20;
			
			resize( null );
		}
		
		private function resize( event: Event ): void
		{
			_peak.x = ( stage.stageWidth - 48 ) >> 1;
			_peak.y = ( stage.stageHeight ) >> 1;
		}

		private function enterFrame( event: Event ): void
		{
			var driver: AudioDriver = _engine.driver;
			
			var g: Graphics = _peak.graphics;
			
			g.clear();
			g.beginFill( 0x333333 );
			g.drawRect( 0, 0, 16, -driver.leftPeak * 128 );
			g.endFill();
			g.beginFill( 0x666666 );
			g.drawRect( 32, 0, 16, -driver.rightPeak * 128 );
			g.endFill();
		}
	}
}
